﻿using CefSharp;
using System;
using System.IO;

namespace web_frame
{
    class MyResponseFilter : IResponseFilter
    {
        public event LoadResponseDataEvent LoadResponseData;

        bool IResponseFilter.InitFilter()
        {
            return true;
        }
        void IDisposable.Dispose()
        {
             
        }

        FilterStatus IResponseFilter.Filter(Stream dataIn, out long dataInRead, Stream dataOut, out long dataOutWritten)
        {
            if (dataIn == null)
            {
                dataInRead = 0;
                dataOutWritten = 0;
                return FilterStatus.Done;
            }

            try
            {
                dataInRead = dataIn.Length;
                dataOutWritten = Math.Min(dataInRead, dataOut.Length);
                dataIn.CopyTo(dataOut);

                MemoryStream stream = new MemoryStream();
                dataIn.Position = 0;
                dataIn.CopyTo(stream);
                LoadResponseData?.Invoke(stream.ToArray());
                return FilterStatus.Done;
            }
            catch
            {
                dataInRead = 0;
                dataOutWritten = 0;
                return FilterStatus.Done;
            }
        }
    }
}